package com.health.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.health.domain.pojo.Setmeal;
import com.health.domain.query.PageQueryDTO;
import com.health.domain.vo.PageBean;
import com.health.domain.vo.Result;
import com.health.mapper.SetmealCheckgroupMapper;
import com.health.mapper.SetmealMapper;
import com.health.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ghy
 * @version 1.0
 * @since 2025-06-22 08:14
 */
@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealCheckgroupMapper setmealCheckgroupMapper;

    /**
     * 分页条件查询
     *
     * @param pageQueryDTO 分页参数
     * @return
     */
    @Override
    public PageBean<Setmeal> findPage(PageQueryDTO pageQueryDTO) {
        // 1.设置分页参数
        Integer currentPage = pageQueryDTO.getCurrentPage();
        Integer pageSize = pageQueryDTO.getPageSize();
        String queryString = pageQueryDTO.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        // 2.执行查询
        List<Setmeal> setmealList = setmealMapper.selectList(queryString);
        // 3.封装数据
        PageInfo<Setmeal> pageInfo = new PageInfo<>(setmealList);
        return new PageBean<>(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public Result<String> add(Setmeal setmeal, Integer[] checkgroupIds) {

        // 保存套餐信息
        setmealMapper.insert(setmeal);

        // 保存套餐与检查组关联信息
        setmealCheckgroupMapper.insertBatch(setmeal.getId(), checkgroupIds);
        return Result.success("新增套餐成功");
    }

    /**
     * 查询所有
     *
     * @return
     */
    @Override
    public Result<List<Setmeal>> getAllSetmeal() {
        List<Setmeal> setmealList = setmealMapper.selectList(null);
        return Result.success(setmealList);
    }

    @Override
    public Result<Setmeal> findById(Integer id) {
        Setmeal setmeal = setmealMapper.selectByIdWithCheckgroupsAndCheckitems(id);
        return Result.success(setmeal);
    }
}
